<?php
/**
  *@function 得到所有的分类信息
  *
  *@date 2013-05-29 20:17
  *
  */
class M_bookmark extends CI_Model {
	
	private $table_name = "zy_bookmark";

    function __construct()
    {
        parent::__construct();
		$this->load->database();
    }
    
    /**
     * 将用户的收藏加入数据库
     * 
     * @param int $book_id 用户收藏的商品ID
     * @param int $user_id 用户的唯一ID
     * 
     * @return boolean
     */
    public function insertBookmark( $book_id , $user_id )
    {
    	$data = array( 'book_id' => $book_id,'user_id'=>$user_id ,'mark_time'=>time());
    	if($this->db->insert( $this->table_name ,$data))
    	{
    		return true;
    	}
    }
    
    /**
     * 获取用户是否已经收藏了该商品
	 * 
	 * @param int $book_id 用户收藏的商品ID
     * @param int $user_id 用户的唯一ID
	 * 
	 * @return 结果集
     */
    public function selectBookmark( $book_id ,$user_id)
    {
    	return $this->db->get_where( $this->table_name ,array('book_id'=>$book_id,'user_id'=>$user_id))->result();
    }
    
    /**
     * 获取用户的书签
     * @param int $user_id 用户的ID
     * @param int $page 记录起始页
     * @param int $pagesize 每页的记录条数
     * 
     * @return 用户书签组成的数组
     */
    public function selectUserBookmark($user_id , $page , $pagesize)
    {
    	$query = "SELECT b.`book_img`,b.`book_name`,b.`book_id`,b.`book_price`,b.`book_store`,bk.*
    			FROM `zy_books` AS b,`{$this->table_name}` AS bk
    			WHERE bk.`book_id`=b.`book_id`
    			AND bk.`user_id`=".$user_id."
    			ORDER BY bk.`mark_id` DESC
    			LIMIT ".$page.",".$pagesize;
    	return $this->db->query($query)->result();
    }
    
    /**
     * 获取用户书签数量
     */
    public function selectuserBookmarkAmount($user_id)
    {
    	$query = "SELECT COUNT(*) AS bookmarkAmount FROM `{$this->table_name}` WHERE `user_id`=".$user_id;
    	return $this->db->query($query)->result();
    	
    	
    }
    
    /**
     * 删除书签
     */
    public function deleteUserBookmark($id)
    {
    	if($this->db->delete($this->table_name,array('mark_id'=>$id,"user_id"=>$this->session->userdata('user_id')))) return true;
    }
	
	/**
	 * 记录用户操作日志
	 */
	public function bookmarkLog( $book_id , $user_id , $mark_type)
	{
		$data = array( 'book_id' => $book_id,'user_id'=>$user_id ,'mark_time'=>time(),'mark_type'=>$mark_type);
    	if($this->db->insert( 'zy_bookmark_log' ,$data))
    	{
    		return true;
    	}
	}
    

    
    

}